package com.ln.aop01.aspect;

import com.ln.aop01.annotation.MyPermission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/12 16:01
 */
@Component
@Aspect
public class PermissionAop {
    @Pointcut("@annotation(com.ln.aop01.annotation.MyPermission)")
    public void permissionPointcut(){};
    private List<String> list=new ArrayList<String>();
    public PermissionAop(){
        list.add("add");
        list.add("delete");
    }
    @Around("permissionPointcut()")
    public Object bdfore(ProceedingJoinPoint joinPoint) throws Throwable{
        //得到连接点执行的方法对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        //方法上的注解对象
        MyPermission annotation = method.getAnnotation(MyPermission.class);
        if (annotation!=null){
            //获取注解的属性value值
            String value = annotation.value();
            if(!list.contains(value)){
                return "权限不足";
            }
        }
        return joinPoint.proceed();
    }
}
